package com.dongdong.zhuangji.utils;

import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 *
 */
@Component
public class StringToDateConverter implements Converter<String, Date> {

    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    @Override
    public Date convert(String source) {
        if (source == null || source.isEmpty()) {
            return null;
        }
        try {
            return new SimpleDateFormat(DATE_FORMAT).parse(source);
        } catch (ParseException e) {
            throw new IllegalArgumentException("Invalid date format. Please use " + DATE_FORMAT);
        }
    }
    public static void main(String[] args) {
        Date date = new Date();
        System.out.println(date);

        // 将 Date 转换为 LocalDate
        LocalDate localDate = date.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDate();

        System.out.println(localDate);
        // 提取时间部分并格式化为 HH:mm
        LocalTime localTime = date.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalTime();
        String formattedTime = localTime.format(java.time.format.DateTimeFormatter.ofPattern("HH:mm"));
        System.out.println(formattedTime);

        String timeStr =formattedTime;

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
        LocalTime time = LocalTime.parse(timeStr, formatter);

        // 增加 25 分钟
        LocalTime newTime = time.plusMinutes(25);

        // 格式化输出结果
        String formattedTime1 = newTime.format(formatter);
        System.out.println("增加 25 分钟后的时间: " + formattedTime1);
    }


}